﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTime>
#include <QDateTime>
#include <QDebug>
#include <QMessageBox>
#include <QCheckBox>
#include <QVariant>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QtCharts>
#include <QRandomGenerator>
#include <QDir>

#include "serial_dev.h"
#include "process_logic.h"
#include "ChartView.h"
//#include "core/alg/generator.h"
//#include "core/FFT/czt.h"
#include "tracker1D_api.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

QT_CHARTS_USE_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    #define arraySize 4096U
    #define MODE_LEVEL  0 
    #define MODE_LIQUID 1

    serial_dev serial;
    QStringList port_name;
//    std::unique_ptr<QChartView> chartView;
    ChartView* chartView;
    std::unique_ptr<QChart> chart;
    std::unique_ptr<QLineSeries> series;        //频谱数据
    std::unique_ptr<QLineSeries> series1;        //阈值数据
    std::unique_ptr<QGraphicsTextItem> text;        //标记数据
    std::unique_ptr<QGraphicsTextItem> text1;        //标记数据
    std::unique_ptr<QTimer> charttimer;
    std::unique_ptr<QValueAxis> axisX;
    std::unique_ptr<QValueAxis> axisY;

    std::thread data_generate_thread;
    std::thread data_store_thread;
    std::mutex mtx;
    std::queue<QList<QPointF>> point_data;
    std::queue<QList<QPointF>> threshold_data;
    float data[arraySize] = { 0.0f };

    bool data_ready = false;
    std::condition_variable cv;
    std::unique_ptr<std::vector<uint16_t>> fft_mag_data;
    const float fix = 0.045e-6;
    const float res = 3e8 / 77e9 / 2 / (45e-6 + fix) / 4096;
    //const float res1 = 3e8 / 2 / (2048+6) / 1.883e6;            //理论上是2048us
    const float res1 = 3e8 / 2 / (1024 + 6) / 3.573e6;

    float speed = 0.0f;
    float range = 0.0f;
    float range_temp[16] = { 0.0f };
    tracker1D_output_t* tracker1D_output;
    uint32_t range_temp_num = 0;
    bool range_update_flag = false;
    int maxIndex = 0;
    int maxIndex_1 = 0;
    std::atomic<int> mode = MODE_LIQUID;
    std::map<std::vector<uint16_t>, std::vector<int>> result;

    QString current_time;
    std::condition_variable file_cv;
    bool file_data_ready = false;
    std::mutex file_mtx;

    std::mutex obj_mtx;
    std::condition_variable obj_cv;
    bool obj_data_ready = false;

    void screen_init();
    void display_init();
    void alg_init();
    void callback_register();
    void data_generate_task();
    void data_store_task();
    void obj_store_task();
    void findpeaks(const uint16_t N, 
                   const uint16_t* in, 
                   const uint8_t peak_num, 
                   const uint16_t* peak_index,
                   const uint32_t threshold);


    std::map<std::vector<uint16_t>, std::vector<int>> findpeaks(const std::vector<uint16_t>& vec, const uint16_t peak_num, const uint16_t threshold);

    void mode_handle();
private:
    Ui::MainWindow *ui;


public slots:
    void refresh_driver();
    void driver_control();
    void update_chart();
    void data_display(QByteArray* ptr, uint16_t* devaddr);
    void range_display(QByteArray* ptr);

};
#endif // MAINWINDOW_H
